package com.irdstudio.apicenter.console.controller.appmgr;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.irdstudio.apicenter.console.dm.AppBaseInfo;
import com.irdstudio.apicenter.console.util.CurrentDateUtil;
import com.irdstudio.apicenter.console.util.MacmsConstants;
import com.irdstudio.apicenter.core.dvo.RspValue;
import com.irdstudio.apicenter.frame.core.jdbc.JdbcBaseDAO;
import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;
import com.irdstudio.apicenter.frame.core.jdbc.util.DataMoveUtil;
import com.irdstudio.apicenter.frame.core.jdbc.util.TableMove;
import com.irdstudio.apicenter.frame.util.BeanUtils;

/**
 * 工程管理相关的DAO操作
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-08-24
 */
public class AppProjectManagerDao {

	/* 文本日志输出对象 */
	private static Logger logger = LoggerFactory
			.getLogger(AppProjectManagerDao.class);

	/**
	 * 创建应用基本信息(根据应用编号与应用名称)
	 * 
	 * @param appId
	 * @param appName
	 * @return
	 */
	public RspValue createAppBaseInfo(String appId, String appName) {

		RspValue rspValue = new RspValue();

		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		AppBaseInfo appBaseInfoBean = new AppBaseInfo();
		appBaseInfoBean.setAppId(appId);
		appBaseInfoBean.setAppName(appName);
		appBaseInfoBean
				.setAppProjectState(MacmsConstants.APP_PROJECT_STATE_UNINIT);
		appBaseInfoBean.setAppProjectDate(CurrentDateUtil.getTodayDate());
		appBaseInfoBean.setAppState(MacmsConstants.APP_STATE_OK);
		baseDAO.insertBean(appBaseInfoBean);
		return rspValue;
	}

	/**
	 * 根据选项复制工程配置
	 * 
	 * @param srcAppId
	 * @param targetAppId
	 * @param appOption
	 * @return
	 */
	public RspValue copyProjectConf(String srcAppId, String targetAppId,
			String appOption) {
		int option = NumberUtils.toInt(appOption);
		RspValue rspValue = null;
		switch (option) {
		case MacmsConstants.APP_OPTION_BASE:
			rspValue = copyAppBaseConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_CERT:
			rspValue = copyAppCertConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_STYLE:
			rspValue = copyAppStyleConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_COMM:
			rspValue = copyAppCommConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_MODULE:
			rspValue = copyAppModuleConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_TRADE:
			rspValue = copyAppTradeConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_RESOURCE:
			rspValue = copyAppResourceConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_AUTH:
			rspValue = copyAppAuthConf(srcAppId, targetAppId);
			break;
		case MacmsConstants.APP_OPTION_DEVICE:
			rspValue = copyAppDeviceConf(srcAppId, targetAppId);
			break;
		}
		if (rspValue == null)
			rspValue = new RspValue("复制工程配置异常");
		return rspValue;
	}

	/**
	 * 删除应用下的配置信息
	 * 
	 * @param targetAppId
	 * @return
	 */
	public RspValue deleteProjectConf(String targetAppId) {
		logger.info("删除应用[" + targetAppId + "]下的配置信息!");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_base_info where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_user where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_org where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_role where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_duty where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_user_role where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_user_duty where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_menu_resource where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_role_right where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_device_register where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_resource where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_channel_forward where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_trade_private where app_id=?",
				new Object[] { targetAppId });
		baseDAO
				.update(
						"delete from app_trade_inout where trade_id in(select trade_id from app_trade_private where app_id=?)",
						new Object[] { targetAppId });
		baseDAO.update("delete from app_frame_module where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_module_param where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_frame_comm where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_channel_access where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_frame_style where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_frame_certificate where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_frame_start where app_id=?",
				new Object[] { targetAppId });
		return new RspValue();
	}

	private RspValue copyAppDeviceConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]设备准入设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_device_register where app_id=?",
				new Object[] { targetAppId });
		return copyAppTableData(srcAppId, targetAppId, "app_device_register");
	}

	private RspValue copyAppAuthConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]统一认 证设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_user where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_org where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_role where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_duty where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_user_role where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_user_duty where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_menu_resource where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_role_right where app_id=?",
				new Object[] { targetAppId });
		RspValue rspValue = copyAppTableData(srcAppId, targetAppId, "app_user");
		if (!rspValue.isOK())
			return rspValue;
		rspValue = copyAppTableData(srcAppId, targetAppId, "app_org");
		if (!rspValue.isOK())
			return rspValue;
		rspValue = copyAppTableData(srcAppId, targetAppId, "app_role");
		if (!rspValue.isOK())
			return rspValue;
		rspValue = copyAppTableData(srcAppId, targetAppId, "app_duty");
		if (!rspValue.isOK())
			return rspValue;
		rspValue = copyAppTableData(srcAppId, targetAppId, "app_user_role");
		if (!rspValue.isOK())
			return rspValue;
		rspValue = copyAppTableData(srcAppId, targetAppId, "app_user_duty");
		if (!rspValue.isOK())
			return rspValue;
		rspValue = copyAppTableData(srcAppId, targetAppId, "app_menu_resource");
		if (!rspValue.isOK())
			return rspValue;
		return copyAppTableData(srcAppId, targetAppId, "app_role_right");
	}

	private RspValue copyAppResourceConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]资源设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_resource where app_id=?",
				new Object[] { targetAppId });
		return copyAppTableData(srcAppId, targetAppId, "app_resource");
	}

	private RspValue copyAppTradeConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]交易设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_channel_forward where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_trade_private where app_id=?",
				new Object[] { targetAppId });
		baseDAO
				.update(
						"delete from app_trade_inout where trade_id in(select trade_id from app_trade_private where app_id=?)",
						new Object[] { targetAppId });
		return copyAppTradeTableData(srcAppId, targetAppId);
	}

	private RspValue copyAppModuleConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]模块设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_frame_module where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_module_param where app_id=?",
				new Object[] { targetAppId });
		RspValue rspValue = copyAppTableData(srcAppId, targetAppId,
				"app_frame_module");
		if (!rspValue.isOK())
			return rspValue;
		return copyAppTableData(srcAppId, targetAppId, "app_module_param");
	}

	private RspValue copyAppCommConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]通讯设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_frame_comm where app_id=?",
				new Object[] { targetAppId });
		baseDAO.update("delete from app_channel_access where app_id=?",
				new Object[] { targetAppId });
		RspValue rspValue = copyAppTableData(srcAppId, targetAppId,
				"app_frame_comm");
		if (!rspValue.isOK())
			return rspValue;
		return copyAppTableData(srcAppId, targetAppId, "app_channel_access");
	}

	private RspValue copyAppStyleConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]风格设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_frame_style where app_id=?",
				new Object[] { targetAppId });
		return copyAppTableData(srcAppId, targetAppId, "app_frame_style");
	}

	private RspValue copyAppCertConf(String srcAppId, String targetAppId) {
		logger.info("复制应用[" + srcAppId + "]证书设置到应用[" + targetAppId + "]");
		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_frame_certificate where app_id=?",
				new Object[] { targetAppId });
		return copyAppTableData(srcAppId, targetAppId, "app_frame_certificate");
	}

	private RspValue copyAppBaseConf(String srcAppId, String targetAppId) {

		logger.info("复制应用[" + srcAppId + "]基本设置到应用[" + targetAppId + "]");

		JdbcBaseDAO baseDAO = BeanUtils.getBean(JdbcBaseDAO.class);
		baseDAO.update("delete from app_frame_start where app_id=?",
				new Object[] { targetAppId });

		return copyAppTableData(srcAppId, targetAppId, "app_frame_start");
	}

	/**
	 * 复根应用ID复制表数据
	 * 
	 * @param srcAppId
	 * @param targetAppId
	 * @param tableName
	 * @return
	 */
	private RspValue copyAppTableData(String srcAppId, String targetAppId,
			String tableName) {
		Connection conn = ConnectionManager.getConnection();
		TableMove tmMove = null;
		try {
			tmMove = DataMoveUtil.createTableMoveInst(conn, tableName,
					tableName);
			tmMove.setCondition(" where app_id='" + srcAppId + "'");
			tmMove.addValueMapping("app_id", targetAppId);
			tmMove.executeCopy();
		} catch (SQLException e) {
			e.printStackTrace();
			return new RspValue(e.getMessage());
		} finally {
			ConnectionManager.releaseConnection(conn);
		}
		return new RspValue();
	}

	/**
	 * 复制应用下的交易数据到另一个应用下
	 * 
	 * @param srcAppId
	 * @param targetAppId
	 * @return
	 */
	private RspValue copyAppTradeTableData(String srcAppId, String targetAppId) {
		Connection conn = ConnectionManager.getConnection();
		TableMove tmMove = null;
		try {

			// 复制转发渠道配置信息
			tmMove = DataMoveUtil.createTableMoveInst(conn,
					"app_channel_forward", "app_channel_forward");
			tmMove.setCondition(" where app_id='" + srcAppId + "'");
			tmMove.addFieldMapping("forward_channel_id",
					"concat(forward_channel_id,'_" + targetAppId + "')");
			tmMove.addValueMapping("app_id", targetAppId);
			tmMove.executeCopy();

			// 复制应用交易配置信息
			tmMove = DataMoveUtil.createTableMoveInst(conn,
					"app_trade_private", "app_trade_private");
			tmMove.setCondition(" where app_id='" + srcAppId + "'");
			tmMove.addFieldMapping("trade_id", "replace(trade_id,'" + srcAppId
					+ "','" + targetAppId + "')");
			tmMove.addFieldMapping("forward_channel_id",
					"concat(forward_channel_id,'_" + targetAppId + "')");
			tmMove.addValueMapping("app_id", targetAppId);
			tmMove.executeCopy();

			// 复制应用交易要素配置信息
			tmMove = DataMoveUtil.createTableMoveInst(conn, "app_trade_inout",
					"app_trade_inout");
			tmMove
					.setCondition(" where trade_id in(select trade_id from app_trade_private where app_id='"
							+ srcAppId + "')");
			tmMove.addFieldMapping("record_key", "uuid()");
			tmMove.addFieldMapping("trade_id", "replace(trade_id,'" + srcAppId
					+ "','" + targetAppId + "')");
			tmMove.executeCopy();

		} catch (SQLException e) {
			e.printStackTrace();
			return new RspValue(e.getMessage());
		} finally {
			ConnectionManager.releaseConnection(conn);
		}
		return new RspValue();
	}

}
